package com.huobao.yunying.utils;

import java.util.ArrayList;
import java.util.List;

import com.huobao.yunying.entity.Regions;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

public class SelectAreaPopupWindow {

	private static PopupWindow m_PopupWindow;
	private static ListView m_ListView;

	private static void initPopupWindow(Context context, int width) {
		if(width == 0)
			width = ScreenUtil.getInstant().getDisWidth(context) - 20;
		m_PopupWindow = new PopupWindow(context);
		m_PopupWindow.setFocusable(true);
		m_PopupWindow.setTouchable(true);
		m_PopupWindow.setOutsideTouchable(true);
		m_PopupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.mipmap.pinglun_bg));
		m_PopupWindow.setWidth(width);
		m_PopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
	}

	private static void createListView(Context context, List<Regions> list, OnItemClickListener listener) {
		m_ListView = new ListView(context);
		android.widget.AbsListView.LayoutParams layoutParams = new android.widget.AbsListView.LayoutParams(
				android.widget.AbsListView.LayoutParams.MATCH_PARENT,
				android.widget.AbsListView.LayoutParams.WRAP_CONTENT);
		m_ListView.setLayoutParams(layoutParams);
		m_ListView.setScrollingCacheEnabled(false);
		m_ListView.setOnItemClickListener(listener);
		if (list == null) {
			list = new ArrayList<Regions>();
			Regions region = new Regions();
			region.region_name = "中国";
			region.isSelect = true;
			list.add(region);
		}
		TextAdapter m_Adapter = new TextAdapter(context, list);
		m_ListView.setAdapter(m_Adapter);
		for (int i = 0; i < list.size(); i++) {
			Regions regions = list.get(i);
			if (regions.isSelect) {
				m_ListView.setSelection(i);
				return;
			}
		}
	}

	public static void dismiss() {
		if (m_PopupWindow != null && m_PopupWindow.isShowing())
			m_PopupWindow.dismiss();
	}

	public static void showPop(Context context, List<Regions> list, View dropView, OnItemClickListener listener) {
		initPopupWindow(context, dropView.getWidth());
		createListView(context, list, listener);
		m_PopupWindow.setContentView(m_ListView);
		m_PopupWindow.showAsDropDown(dropView);
	}

	private static class TextAdapter extends QuickAdapter<Regions> {

		public TextAdapter(Context context, List<Regions> data) {
			super(context, R.layout.item_pop_listview, data);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void convert(BaseAdapterHelper arg0, Regions bean) {
			TextView textView = (TextView) arg0.getView();
			textView.setGravity(Gravity.CENTER);
			String str = bean.region_name;
			textView.setText(str);
			if (bean.isSelect) {
				textView.setTextColor(Color.parseColor("#008bcc"));
			} else {
				textView.setTextColor(Color.parseColor("#000000"));
			}

		}
	}

}
